package bible.sunflower.work.yinfeng.ui;

import java.util.HashMap;
import java.util.Map;

public class Expr {
	
	public static final String OP_EQUAL = "=";
	
	public static final String OP_LIKE = "like";
	
	public static final String OP_IN = "in";
	
	public static final String OP_BETWEEN = "between";
	
	public static final String OP_LESS = "<";
	
	public static final String OP_GREATER = ">";
	
    public static final String OP_LESSEQUAL = "<=";
	
	public static final String OP_GREATEREQUAL = ">=";
	
	public static final String LIKERULE_ALL = "all";
	
	public static final String LIKERULE_START = "start";
	
	public static final String LIKERULE_END = "end";
	
	public static final String LIKERULE_NONE = "none";
	
	private String field;
	
	private String op;
	
	private String likeRule;
	
	private String value = "";
	
	private String min;
	
	private String max;
	
	private String timePattern = "yyyy-MM-dd";
	
	private Boolean isTimeExpr = false;
	
	public Expr( String field ){
		this.field = field;
	}
	
	public Expr( String field, String value ){
		this.field = field;
		this.value = value;
	}
	
	public Expr( String field, String op, String value){
		this.field = field;
		this.op = op;
		this.value = value;
	}
	
	public static Expr createLikeExpr( String field, String likeRule, String value ){
		Expr expr = new Expr( field, value );
		expr.setOp( Expr.OP_LIKE );
		expr.setLikeRule( likeRule );
		return expr;
	}
	
	public static Expr createBetweenExpr( String field, String min, String max ){
		Expr expr = new Expr( field );
		expr.setOp( Expr.OP_BETWEEN );
		expr.setMax(max);
		expr.setMin(min);
		return expr;
	}
	 
	public static Expr createTimeExpr( String field, String min, String max ){
		Expr expr = new Expr( field );
		expr.setOp( Expr.OP_BETWEEN );
		expr.setMax(max);
		expr.setMin(min);
		expr.setIsTimeExpr( true );
		return expr;
	}
	
	public Map<String, Object> getRequestParam( int index ){
		String prefix = "criteria/_expr[" + index + "]/";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put( prefix + "_op", op);
		if( Expr.OP_LIKE.equalsIgnoreCase( op ) ){
			paramMap.put( prefix + field, value );
			paramMap.put( prefix + "_likeRule", likeRule );
		}else if( Expr.OP_BETWEEN.equalsIgnoreCase( op ) ){
			paramMap.put( prefix + field, "");
			paramMap.put( prefix + "_min", min);
			paramMap.put( prefix + "_max", max);
			if( this.isTimeExpr ){
				paramMap.put( prefix + "_pattern", timePattern );
			}
		}else{
			paramMap.put( prefix + field, value );
		}
		return paramMap;
	}

	public String getField() {		
		return field;
	}

	public void setField(String field) {
		this.field = field;
	}

	public String getOp() {
		return op;
	}

	public void setOp(String op) {
		this.op = op;
	}

	public String getLikeRule() {
		return likeRule;
	}

	public void setLikeRule(String likeRule) {
		this.likeRule = likeRule;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getMin() {
		return min;
	}

	public void setMin(String min) {
		this.min = min;
	}

	public String getMax() {
		return max;
	}

	public void setMax(String max) {
		this.max = max;
	}

	public String getTimePattern() {
		return timePattern;
	}

	public void setTimePattern(String timePattern) {
		this.timePattern = timePattern;
	}

	public Boolean getIsTimeExpr() {
		return isTimeExpr;
	}

	public void setIsTimeExpr(Boolean isTimeExpr) {
		this.isTimeExpr = isTimeExpr;
	}
	
	

}
